package mxmf.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class HanyuPinyinHelper {

    private HanyuPinyinHelper() {
    }

    private static final String NUMBER_0_9 = "[0-9]+";
    private static final String A_Z = "[a-zA-Z]+";

    private static final String CHINESE_WROD = "[\u4e00-\u9fa5]+";

    private static final Logger logger = LoggerFactory.getLogger(HanyuPinyinHelper.class);

    /** 
     * 将文字转为汉语拼音
     * @param chineselanguage 要转成拼音的中文
     */
    public static String toHanyuPinyin(String chineseLanguage) {
        char[] clChars = chineseLanguage.trim().toCharArray();
        StringBuilder hanyupinyin = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
        defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        try {
            for (int i = 0; i < clChars.length; i++) {
                if (String.valueOf(clChars[i]).matches(CHINESE_WROD)) {// 如果字符是中文,则将中文转为汉语拼音
                    hanyupinyin.append(
                            PinyinHelper.toHanyuPinyinStringArray(clChars[i], defaultFormat)[0]);
                } else {// 如果字符不是中文,则不转换
                    hanyupinyin.append(clChars[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            logger.error(e.toString(), e);
        }
        return hanyupinyin.toString();
    }

    public static String getFirstLettersUp(String chineseLanguage) {
        return getFirstLetters(chineseLanguage, HanyuPinyinCaseType.UPPERCASE);
    }

    public static String getFirstLettersLo(String chineseLanguage) {
        return getFirstLetters(chineseLanguage, HanyuPinyinCaseType.LOWERCASE);
    }

    public static String getFirstLetters(String chineseLanguage, HanyuPinyinCaseType caseType) {
        char[] clChars = chineseLanguage.trim().toCharArray();
        StringBuilder hanyupinyin = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(caseType);// 输出拼音全部大写
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
        for (int i = 0; i < clChars.length; i++) {
            String str = String.valueOf(clChars[i]);
            if (str.matches(CHINESE_WROD)) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
                try {
                    String letter = PinyinHelper.toHanyuPinyinStringArray(clChars[i],
                            defaultFormat)[0].substring(0, 1);
                    hanyupinyin.append(letter);
                } catch (Exception e) {
                    logger.error(e.toString(), e);
                }
            } else if (str.matches(NUMBER_0_9)) {// 如果字符是数字,取数字
                hanyupinyin.append(clChars[i]);
            } else if (str.matches(A_Z)) {// 如果字符是字母,取字母
                hanyupinyin.append(clChars[i]);
            } else {// 否则不转换
                hanyupinyin.append(clChars[i]);
                // 如果是标点符号的话，带着
            }
        }
        return hanyupinyin.toString();
    }

    public static String getPinyinString(String chineseLanguage) {
        char[] clChars = chineseLanguage.trim().toCharArray();
        StringBuilder hanyupinyin = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部大写
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
        try {
            for (int i = 0; i < clChars.length; i++) {
                String str = String.valueOf(clChars[i]);
                if (str.matches(CHINESE_WROD)) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
                    hanyupinyin.append(
                            PinyinHelper.toHanyuPinyinStringArray(clChars[i], defaultFormat)[0]);
                } else if (str.matches(NUMBER_0_9)) {// 如果字符是数字,取数字
                    hanyupinyin.append(clChars[i]);
                } else if (str.matches(A_Z)) {// 如果字符是字母,取字母
                    hanyupinyin.append(clChars[i]);
                } else {// 否则不转换
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            logger.error(e.toString(), e);
        }
        return hanyupinyin.toString();
    }

    /**
     * 取第一个汉字的第一个字符
    * @Title: getFirstLetter 
    * @Description: 
    * @return String   
    * @throws
     */
    public static String getFirstLetter(String chineseLanguage) {
        char[] clChars = chineseLanguage.trim().toCharArray();
        String hanyupinyin = "";
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);// 输出拼音全部大写
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
        String str = String.valueOf(clChars[0]);
        if (str.matches(CHINESE_WROD)) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
            try {
                hanyupinyin = PinyinHelper.toHanyuPinyinStringArray(clChars[0], defaultFormat)[0]
                        .substring(0, 1);
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                logger.error(e.toString(), e);

            }
        } else if (str.matches(NUMBER_0_9)) {// 如果字符是数字,取数字
            hanyupinyin += clChars[0];
        } else if (str.matches(A_Z)) {// 如果字符是字母,取字母

            hanyupinyin += clChars[0];
        } else {// 否则不转换

        }
        return hanyupinyin;
    }

}
